package de.ur.iw.wiki.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import de.ur.iw.sql.WikiSQLHelper;
import de.ur.iw.wiki.PathThread;
import de.ur.iw.wiki.Settings;
import de.ur.iw.wiki.WikiPage;

public class PathTester {

	private static int paths = 10;

	private static final int NUM_THREADS = 32;

	private static final String version = "1.01";

	private static int pathcount = 0;

	private static int done = 0;

	private static WikiSQLHelper database;

	/**
	 * if true the program wont stop but restart the finished threads!
	 */
	private static boolean loop = false;

	private static String url = "localhost", username = "root", password = "",
			tablename = "paths";

	public static void main(String[] args) {

		System.out.println("Wikipedia RandomWalk Simulator v" + version);
		System.out
				.println("by Bastian Hinterleitner (bastian.hinterleitner@gmail.com)");

		init();

		System.out.println("working ...");
		try {

			database = new WikiSQLHelper(url, username, password, tablename);
			database.connect();
			for (int i = 0; i < paths; i++) {
				new PathThread((int) Settings.getDouble(
						Settings.KEY_LIMITED_STEPS, 100))
						.start();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		done();
	}

	private static void init() {
		url = readString("please enter the location of your sql-database: ");
		username = readString("please enter the username to your sql-database: ");
		password = readString("please enter the password for that username: ");
		WikiPage.setLanguage(readString("finally please enter the language code (de for german wikipedia, en for english ...): "));
		String ok = "";
		while (!ok.equals("y")) {
			ok = readString(
					"a database '" + WikiSQLHelper.DBNAME + "' and table '"
							+ tablename + "' will be created, ok? (y/n): ")
					.toLowerCase();
			if (ok.equals("n")) {
				System.out.println("exit ...");
				System.exit(0);
			}
		}
		paths = readInt("please enter the number of paths to simulate (0 for endless): ");
		if (paths <= 0) {
			loop = true;
			paths = NUM_THREADS;
		}
	}

	private static void done() {
		if (loop) {
			System.out.println(pathcount + " paths done");
			new PathThread((int) Settings.getDouble(
					Settings.KEY_LIMITED_STEPS, 100)).start();
		} else {
			System.out.println(progress((int) (10000 * ++done / paths) / 100));
			if (--pathcount <= 0) {
				System.out.println("... done");
			}
		}
	}

	private static String progress(double progress) {
		String prog = progress + "% [";
		int p = (int) progress / 2;
		for (int i = 0; i < 50; i++) {
			if (i <= p) {
				prog += "#";
			} else {
				prog += "_";
			}
		}
		prog += "]";
		return prog;
	}

	private static String readString(String str) {
		System.out.print(str);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			return in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	private static int readInt(String str) {
		String result = readString(str);
		try {
			return Integer.parseInt(result);
		} catch (NumberFormatException e) {
			return readInt(str);
		}
	}
}
